listlistmodel: Add gtk_list_list_model_item_moved()
authorBenjamin Otte <otte@redhat.com>
Fri, 28 Sep 2018 00:45:54 +0000 (02:45 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 30 May 2020 23:26:45 +0000 (19:26 -0400)
Use it to fix a case that just said g_warning ("oops").

Apparently I had forgotten the case where a container moved a child
in the widget tree.

gtk/gtklistlistmodel.c
gtk/gtklistlistmodelprivate.h
gtk/gtkwidget.c

index 3d10f4928089da7b25158c61ad54f7ad20ce61bb..f01133978dec17059828f0c596cb592a75459d0b 100644 (file)
@@ -206,15 +206,12 @@ gtk_list_list_model_new_with_size (GType          item_type,
   return result;
 }
 
-void
-gtk_list_list_model_item_added (GtkListListModel *self,
-                                gpointer          item)
+static guint
+gtk_list_list_model_find (GtkListListModel *self,
+                          gpointer          item)
 {
-  gpointer x;
   guint position;
-
-  g_return_if_fail (GTK_IS_LIST_LIST_MODEL (self));
-  g_return_if_fail (item != NULL);
+  gpointer x;
 
   position = 0;
   for (x = self->get_first (self->data);
@@ -222,7 +219,17 @@ gtk_list_list_model_item_added (GtkListListModel *self,
        x = self->get_next (x, self->data))
     position++;
 
-  gtk_list_list_model_item_added_at (self, position);
+  return position;
+}
+
+void
+gtk_list_list_model_item_added (GtkListListModel *self,
+                                gpointer          item)
+{
+  g_return_if_fail (GTK_IS_LIST_LIST_MODEL (self));
+  g_return_if_fail (item != NULL);
+
+  gtk_list_list_model_item_added_at (self, gtk_list_list_model_find (self, item));
 }
 
 void
@@ -241,26 +248,49 @@ void
 gtk_list_list_model_item_removed (GtkListListModel *self,
                                   gpointer          previous)
 {
-  gpointer x;
   guint position;
 
   g_return_if_fail (GTK_IS_LIST_LIST_MODEL (self));
 
   if (previous == NULL)
+    position = 0;
+  else
+    position = 1 + gtk_list_list_model_find (self, previous);
+
+  gtk_list_list_model_item_removed_at (self, position);
+}
+
+void
+gtk_list_list_model_item_moved (GtkListListModel *self,
+                                gpointer          item,
+                                gpointer          previous_previous)
+{
+  guint position, previous_position;
+  guint min, max;
+
+  g_return_if_fail (GTK_IS_LIST_LIST_MODEL (self));
+  g_return_if_fail (item != previous_previous);
+
+  position = gtk_list_list_model_find (self, item);
+
+  if (previous_previous == NULL)
     {
-      position = 0;
+      previous_position = 0;
     }
   else
     {
-      position = 1;
-
-      for (x = self->get_first (self->data);
-           x != previous;
-           x = self->get_next (x, self->data))
-        position++;
+      previous_position = gtk_list_list_model_find (self, previous_previous);
+      if (position > previous_position)
+        previous_position++;
     }
 
-  gtk_list_list_model_item_removed_at (self, position);
+  /* item didn't move */
+  if (position == previous_position)
+    return;
+
+  min = MIN (position, previous_position);
+  max = MAX (position, previous_position) + 1;
+  g_list_model_items_changed (G_LIST_MODEL (self), min, max - min, max - min);
 }
 
 void
index 3103b3c7c3c134a9ebe4d129a7ef696d97577b5b..f0a57172387aa1c6d7fde433a163b704fe8e7042 100644 (file)
@@ -64,6 +64,9 @@ void                    gtk_list_list_model_item_removed        (GtkListListMode
                                                                  gpointer                previous);
 void                    gtk_list_list_model_item_removed_at     (GtkListListModel       *self,
                                                                  guint                   position);
+void                    gtk_list_list_model_item_moved          (GtkListListModel       *self,
+                                                                 gpointer                item,
+                                                                 gpointer                previous_previous);
 
 void                    gtk_list_list_model_clear               (GtkListListModel       *self);
 
index f667a5d8d42d7ea2f495f2580f60ac3aa7d1421f..32286e9d4dbbc62a24bdf1f082b79dd1df03e3a2 100644 (file)
@@ -5881,7 +5881,7 @@ gtk_widget_reposition_after (GtkWidget *widget,
   if (parent->priv->children_observer)
     {
       if (prev_previous)
-        g_warning ("oops");
+        gtk_list_list_model_item_moved (parent->priv->children_observer, widget, prev_previous);
       else
         gtk_list_list_model_item_added (parent->priv->children_observer, widget);
     }